package com.hnqcgc.service;

import com.hnqcgc.dao.IProductDao;
import com.hnqcgc.dao.ProductDaoImpl;
import com.hnqcgc.db.MyTransaction;
import com.hnqcgc.vo.TProduct;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/*
产品服务实现类
 */
public class ProductServiceImpl implements IProductService{
    //定义事务
    MyTransaction myTransaction=new MyTransaction(true);
    //定义dao
    IProductDao productDao=new ProductDaoImpl(myTransaction);
    @Override
    public List<TProduct> getProductList(int count) {
        //定义集合接收dao接口返回数据
        List<TProduct> list=new ArrayList<TProduct>();
        try {
            list=productDao.getAnyProduct(count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //释放连接资源
        myTransaction.closeConn();
        //返回接口数据
        return list;
    }

    @Override
    public TProduct getProductInfoById(int pid) {
        TProduct p = null;
        try {
            p = productDao.getProductInfoById(pid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //关闭数据库连接
        myTransaction.closeConn();
        return p;
    }
}
